package uri.model.messenger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import uri.model.MemberVO;
import uri.model.MessageVO;

@Service
public class MessengerServiceImpl implements MessengerService {
	@Resource(name="messengerDAOImpl")
	private MessengerDAO messengerDAO;

	@Override
	public List<MemberVO> findFriend(String findType, String findWord, String loginId) {
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("findType", findType);
		map.put("findWord", findWord);
		map.put("loginId", loginId);
		return messengerDAO.findFriend(map);
	}

	@Override
	public void addFriend(String loginId, String friendId) throws AlreadyFriendException {
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("loginId", loginId);
		map.put("friendId", friendId);
		if(messengerDAO.isFriend(map)!=0){ // 이미 친구인 상태
			throw new AlreadyFriendException("이미 친구 입니다.");
		}
		messengerDAO.addFriend(map);
	}

	@Override
	public ListVO<MemberVO> friendList(HashMap<String,String> map) {
		ListVO<MemberVO> lvo=new ListVO<MemberVO>();
		String page=map.get("page");
		if(page==null || page.equals("null") || page=="0" || page==""){
			page="1";
			map.put("page", page);
		}
		lvo.setList(messengerDAO.friendList(map));
		lvo.setPagingBean(new PagingBean(messengerDAO.getTotalFriends(map.get("loginId")), Integer.parseInt(page)));
		return lvo;
	}
	
	@Override
	public ListVO<MemberVO> memberList(HashMap<String, String> map) {
		ListVO<MemberVO> lvo=new ListVO<MemberVO>();
		String page=map.get("page");
		if(page==null || page.equals("null") || page=="0" || page==""){
			page="1";
			map.put("page", page);
		}
		lvo.setList(messengerDAO.memberList(map));
		lvo.setPagingBean(new PagingBean(messengerDAO.getTotalMembers(), Integer.parseInt(page)));
		return lvo;
	}

	@Override
	public void deleteFriend(String loginId, String friend) {
		String[] friendArr=friend.split(",");
		for(int i=0; i<friendArr.length; i++){
			HashMap<String,String> map=new HashMap<String,String>();
			map.put("loginId", loginId);
			map.put("friendId", friendArr[i]);
			messengerDAO.deleteFriend(map);
		}
	}

	@Override
	public void sendMessage(MessageVO msgvo, ArrayList<HashMap<String,String>> nameList) {
		String[] receiversArr=msgvo.getReceiver().split(",");
		for(int i=0; i<receiversArr.length; i++){
			/*map.put("loginId", msgvo.getSender());
			map.put("content", msgvo.getContent());
			map.put("receiver", receiversArr[i]);*/
			msgvo.setReceiver(receiversArr[i]);
			messengerDAO.sendMessage(msgvo); // no setting
			for(int j=0; j<nameList.size(); j++){
				String no=msgvo.getNo();
				String orgfilename=nameList.get(j).get("orgfilename");
				String newfilename=nameList.get(j).get("newfilename");
				HashMap<String,String> map=new HashMap<String,String>();
				map.put("msgno", no);
				map.put("orgfilename", orgfilename);
				map.put("newfilename", newfilename);
				messengerDAO.messageFileUpload(map);
			} // for
		} // for
	}

	@Override
	public ListVO<MessageVO> sentMessageList(HashMap<String,String> map) {
		ListVO<MessageVO> lvo=new ListVO<MessageVO>();
		String page=map.get("page");
		if(page==null || page.equals("null") || page=="0" || page==""){
			page="1";
			map.put("page", page);
		}
		List<MessageVO> list=messengerDAO.sentMessageList(map);
		for(int i=0; i<list.size(); i++){
			String msgNo=list.get(i).getNo();
			List<HashMap<String,String>> fileList=messengerDAO.getFileList(msgNo); // 메세지에 해당하는 업로드 파일
			list.get(i).setFileList(fileList);
		}
		lvo.setList(list);
		lvo.setPagingBean(new PagingBean(messengerDAO.getTotalSentMessage(map.get("loginId")), Integer.parseInt(page)));
//		System.out.println(lvo);
		return lvo;
	}

	@Override
	public ListVO<MessageVO> receivedMessageList(HashMap<String, String> map) {
		ListVO<MessageVO> lvo=new ListVO<MessageVO>();
		String page=map.get("page");
		if(page==null || page.equals("null") || page=="0" || page==""){
			page="1";
			map.put("page", page);
		}
		List<MessageVO> list=messengerDAO.receivedMessageList(map);
		for(int i=0; i<list.size(); i++){
			String msgNo=list.get(i).getNo();
			List<HashMap<String,String>> fileList=messengerDAO.getFileList(msgNo); // 메세지에 해당하는 업로드 파일
			list.get(i).setFileList(fileList);
		}
		lvo.setList(list);
		lvo.setPagingBean(new PagingBean(messengerDAO.getTotalReceivedMessage(map.get("loginId")), Integer.parseInt(page)));
		return lvo;
	}

	@Override
	public void sendChattingMessage(String id, String chattingMessage) {
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("id", id);
		map.put("chattingMessage", chattingMessage);
		messengerDAO.sendChattingMessage(map);
	}

	@Override
	public List<Map<String, String>> getChatting() {
		return messengerDAO.getChatting();
	}

	@Override
	public void deleteChattingTable() {
		messengerDAO.deleteChattingTable();
	}

}
